#pragma once

#include <condition_variable>
#include "executors.h"
#include "common/threadpool.h"
#include "common/log.h"


namespace evm { namespace messaging {

using namespace common;

struct PooledExecutor : BaseExecutor {
    PooledExecutor(ListenerPtr listener, DispatcherPtr dispatcher, size_t pool_size)
        : BaseExecutor(listener, dispatcher), pool_(pool_size) {}

    ~PooledExecutor() {
        if (!stopped_) {
            stopped_ = true;
            stop();
            wait();
        }
    }

    void start() override;

    void stop() override;

    void wait() override;

private:
    ThreadPool pool_;
    thread poller_;
    atomic<bool> stopped_{true};
};

}}
